//package com.yty.system.config;
//
//
//import com.alibaba.fastjson.JSONObject;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.yty.system.entity.AccountInfo;
//import com.yty.system.entity.DeDuplication;
//import com.yty.system.mapper.AccountInfoMapper;
//import com.yty.system.mapper.DeDuplicationMapper;
//import com.yty.system.service.IAccountInfoService;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
//import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
//import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.messaging.Message;
//import org.springframework.stereotype.Component;
//import org.springframework.transaction.annotation.Transactional;
//
//@Component
//@Slf4j
////@RocketMQTransactionListener(txProducerGroup = "producer_group_bank1")
//public class ProducerTxmsgListener implements RocketMQLocalTransactionListener {
//
//    @Autowired
//    IAccountInfoService accountInfoService;
//
//    @Autowired
//    AccountInfoMapper accountInfoDao;
//
//    @Autowired
//    private DeDuplicationMapper deDuplicationMapper;
//
//    //消息发送成功回调此方法，此方法执行本地事务
//    @Override
//    @Transactional
//    public RocketMQLocalTransactionState executeLocalTransaction(Message message, Object arg) {
//        //解析消息内容
//        try {
//            String jsonString = new String((byte[]) message.getPayload());
//            JSONObject jsonObject = JSONObject.parseObject(jsonString);
//            AccountInfo accountChangeEvent = JSONObject.parseObject(jsonObject.getString("accountChange"), AccountInfo.class);
//            //扣除金额
//            accountInfoService.doUpdateAccountBalance(accountChangeEvent);
//            return RocketMQLocalTransactionState.COMMIT;
//        } catch (Exception e) {
//            log.error("executeLocalTransaction 事务执行失败",e);
//            e.printStackTrace();
//            return RocketMQLocalTransactionState.ROLLBACK;
//        }
//    }
//
//    //此方法检查事务执行状态
//    @Override
//    public RocketMQLocalTransactionState checkLocalTransaction(Message message) {
//        RocketMQLocalTransactionState state;
//        JSONObject jsonObject = JSONObject.parseObject(new String((byte[]) message.getPayload()));
//        AccountInfo accountChangeEvent = JSONObject.parseObject(jsonObject.getString("accountChange"),AccountInfo.class);
//        //事务id
//        String txNo = accountChangeEvent.getTxNo();
//        int isexistTx = deDuplicationMapper.selectCount(new QueryWrapper<DeDuplication>().lambda().eq(DeDuplication::getTxNo,txNo));
//        log.info("回查事务，事务号: {} 结果: {}", accountChangeEvent.getTxNo(),isexistTx);
//        if(isexistTx > 0){
//            state=  RocketMQLocalTransactionState.COMMIT;
//        }else{
//            state=  RocketMQLocalTransactionState.UNKNOWN;
//        }
//        return state;
//    }
//}
//
//
